package com.example.dell.newstart.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.dell.newstart.R;
import com.example.dell.newstart.base.RequestInterface;
import com.example.dell.newstart.base.ServerResponse;
import com.example.dell.newstart.bean.MyPageInfo;
import com.example.dell.newstart.bean.Product;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * created by Dongkai on 2018/11/21
 */

public class HomeAdapter extends RecyclerView.Adapter {
    private static final String TAG = "HomeAdapter";
    /**
     * 轮播图
     */
    private static final int BANNER = 0;
    /**
     * 热销商品
     */
    private static final int HOT = 1;
    /**
     * 推荐商品
     */


    private Context context;


    public HomeAdapter(Context context) {
        this.context = context;
        getHomeProduct();
    }

    //存放banner的产品
    private List<Product> bannerProductList = new ArrayList<>();
    //存放热卖的产品
    private List<Product> hotProductList = new ArrayList<>();
    //存放推荐的产品
    private List<Product> recommendProductList = new ArrayList<>();
    //存放新上的产品
    private List<Product> newPrductList = new ArrayList<>();

    //普通商品
    private List<Product> normalProductList = new ArrayList<>();

    private BannerHolder bannerHolder;
    private HotHolder hotHolder;
//    private RecommendHolder recommendHolder;
//    private NewHolder newHolder;
//    private  NormalHolder normalHolder;

    @Override
    public int getItemViewType(int position) {
        int currentType = BANNER;
        switch (position) {
            case BANNER:
                currentType = BANNER;
                break;
            case HOT:
                currentType = HOT;
                break;
//            case RECOMMEND:
//                currentType = RECOMMEND;
//                break;
//            case NEW:
//                currentType = NEW;
//                break;
//            default:
//                currentType = NORMAL;
        }
        return currentType;
    }

    //各类商品的视图
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        LayoutInflater inflater = LayoutInflater.from( context );
        View view;
        switch (viewType) {
            case BANNER:
                view = inflater.inflate( R.layout.item_type_banner, null );
                return new BannerHolder( view );
            case HOT:
                view = inflater.inflate( R.layout.item_type_hot, null );
                return new HotHolder( view );
//            case RECOMMEND:
//                view = inflater.inflate( R.layout.item_type_recommend, null );
//                return new RecommendHolder( view );
//            case NEW:
//                view = inflater.inflate( R.layout.item_type_new, null );
//                return new NewHolder( view );
//            case NORMAL:
//                view = inflater.inflate( R.layout.home_item_normal, null );
//                return new NormalHolder( view );

        }
        return holder;
    }

    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int itemType = getItemViewType( position );
        switch (itemType) {
            case BANNER:
                bannerHolder = (BannerHolder) holder;
                bannerHolder.setData( bannerProductList );
                break;
            case HOT:
                hotHolder = (HotHolder) holder;
                hotHolder.setData( hotProductList );
                break;


        }
    }

    @Override
    public int getItemCount() {
        return 2;
    }

    class BannerHolder extends RecyclerView.ViewHolder {
        private Banner banner;

        public BannerHolder(View itemView) {
            super( itemView );
            banner = itemView.findViewById( R.id.banner_product );
        }

        public void setData(final List<Product> bannerList) {
            List<String> titles = new ArrayList<>();
            List<String> imageUris = new ArrayList<>();
            for (Product product : bannerList) {
                titles.add( product.getName() );
                //轮播图 图片地址
                imageUris.add( RequestInterface.baseImgPath + product.getMainImage() );
            }
            banner.setBannerTitles( titles );
            banner.setImages( imageUris );
            banner.isAutoPlay( true );
            banner.setImageLoader( new ImageLoader() {
                @Override
                public void displayImage(Context context, Object path, ImageView imageView) {
                    Glide.with( context )
                            .load( path )
                            .into( imageView );
                }
            } );
            banner.setOnBannerListener( new OnBannerListener() {
                @Override
                public void OnBannerClick(int position) {
//                    //获取 录播图 对应位置的商品
        //        Product product = bannerList.get(position);
//
//                    Intent intent = new Intent(context, ProductDetailActivity.class);
//                    //把商品信息从intent传递到 ProductDetailActivity 活动
//                    Bundle bundle = new Bundle();
//                    bundle.putSerializable("product", product);
//                    intent.putExtras(bundle);
//                    context.startActivity(intent);
                }
            } );
            banner.start();
        }
    }


    //hotProductList

    class HotHolder extends RecyclerView.ViewHolder {
        private GridView gridView;

        public HotHolder(View itemView) {
            super( itemView );
            gridView = itemView.findViewById( R.id.gv_hot );

        }

        public void setData(List<Product> products) {
            GoodsAdapter appliancesAdapter = new GoodsAdapter( context, products );
            for (Product product : products) {
                Log.d( TAG, "setData: @@@@@@@@@@@@@@@" + product.toString() );
            }

            gridView.setAdapter( appliancesAdapter );
        }
    }




    private void toastShort(String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }

    private void getHomeProduct() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl( RequestInterface.basePath )
                .addConverterFactory( GsonConverterFactory.create() )
                .build();

        RequestInterface.getProductsList2 getProductsList = retrofit.create( RequestInterface.getProductsList2.class );

        Call<ServerResponse<MyPageInfo<Product>>> call = getProductsList.getProducts( "", null, 1, 19, "" );

        call.enqueue( new Callback<ServerResponse<MyPageInfo<Product>>>() {


            public void onResponse(Call<ServerResponse<MyPageInfo<Product>>> call, Response<ServerResponse<MyPageInfo<Product>>> response) {

             Log.d(TAG, "onResponse: " + response.body().getData());


                if (response.body() == null) {
                    toastShort("网络请求错误");
                }
                //获取返回的数据（就是ServerResponse<T>中的T代表的东西）
                MyPageInfo<Product> pageInfo = response.body().getData();
                //在PageInfo中通过list获取到产品的集合（多个product对象）
                if (pageInfo == null) {
                    return;
                }
                List<Product> productList = pageInfo.getList();
                Log.d( TAG, "onResponse: size " + productList.size() );
                for (Product product : productList) {
                    Log.d( TAG, "onResponse: ***********" + product );
                }
                //清空之前数据
                bannerProductList.clear();
                hotProductList.clear();
                recommendProductList.clear();
                newPrductList.clear();

                int maxItem = productList.size();
                bannerProductList = productList.subList( 0,3 );
                hotProductList = productList.subList( 3,5 );


                //设置数据
                if (bannerHolder != null) {
                    bannerHolder.setData( bannerProductList );
                }
                if (hotHolder != null)
                    hotHolder.setData( hotProductList );
//                if (recommendHolder != null)
//                    recommendHolder.setData( recommendProductList );
//                if (newHolder != null)
//                    newHolder.setData( newPrductList );

                hotHolder.setData( productList );
                for (Product product : productList) {
                    Log.d( TAG, "onResponse: ####product#######" + product.toString() );
                }
            }


            public void onFailure(Call<ServerResponse<MyPageInfo<Product>>> call, Throwable t) {
                Log.d( TAG, "onFailure: " );
                Toast.makeText( context, "网络请求错误" + t.getMessage(), Toast.LENGTH_LONG ).show();
            }
        } );
    }
}
